package com.hbnu.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kittychen
 * @since 2025-11-04 14:31
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        // 1、获取用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 2、真实账号密码
        String realUsername = "admin";
        String realPassword = "123456";

        // 3、校验账号密码
        if (username.equals(realUsername) && password.equals(realPassword)) {
            // 请求转发
            // req.getRequestDispatcher("/successServlet").forward(req, resp);
            // 请求重定向
            resp.sendRedirect("/successServlet");
        } else {
            // 请求转发
            // req.getRequestDispatcher("/failServlet").forward(req, resp);
            // 请求重定向
            resp.sendRedirect("/failServlet");
        }
    }
}
